package club.xinghe.servlet.teacher;

import club.xinghe.biz.ExamBiz;
import club.xinghe.biz.TeacherBiz;
import club.xinghe.entity.Exam;
import club.xinghe.entity.Teacher;
import club.xinghe.util.JsonServlet;
import club.xinghe.util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/teacher/ExamServlet")
public class ExamServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        Teacher teacher = (Teacher) session.getAttribute("teacher");
        int zid = teacher.getZy().getzId();

        boolean n = true;

        String see = request.getParameter("see");
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));
        int pageNum = Integer.parseInt(request.getParameter("pageNum"));
        int eid = Integer.parseInt(request.getParameter("eid"));
        ExamBiz examBiz = new ExamBiz();
        if (eid != 0){
            if (examBiz.delExam(eid)>0){
                n=true;
            }
        }


        if (n){
            int count = examBiz.examCount(zid,see);
            int total = PageUtil.getTotalPages(count, 10);

            List<Exam> examList = examBiz.Exam(see,pageNum,pageSize,zid);

            Map<String,Object> maps = new HashMap<>();
            maps.put("examList",examList);
            maps.put("pageNum",pageNum);
            maps.put("pageSize", PageUtil.PAGE_SIZE);
            maps.put("total",total);
            maps.put("zid",zid);

            JsonServlet.getJson(request,response,maps);

        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
